package com.example.webbrowser.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;

import com.example.webbrowser.Database.WebHistoryDao;
import com.example.webbrowser.R;
import com.example.webbrowser.Utils.MyDatabaseHelper;

public class ShowNewsActivity extends AppCompatActivity {

    private WebView show_news;
    private ImageView collect_news;

    // 添加用户等待显示控件
    private ProgressDialog mDialog;
    private MyDatabaseHelper helper;
    protected Button web_tab_arrow_left_second;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_news);
        initView();
        helper = new MyDatabaseHelper(this, "UserDB.db", null, 1);

        mDialog = new ProgressDialog(ShowNewsActivity.this);
        mDialog.setMessage("玩命加载ing");
        show_news.setWebViewClient(new WebViewClient() {
            //网页加载时的回调
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (!mDialog.isShowing()) {
                    mDialog.show();
                }
            }

            //网页停止加载时的回调
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                //加入历史记录
                Intent intent = getIntent();
                String news_url = intent.getStringExtra("url");
                String news_title = intent.getStringExtra("title");
                WebHistoryDao webHistoryDao = new WebHistoryDao(ShowNewsActivity.this);
                webHistoryDao.add(news_title, news_url);

                // 如果没有显示，则显示
                if (mDialog.isShowing())
                    mDialog.dismiss();
            }
        });
        show_news.getSettings().setJavaScriptEnabled(true);
        Intent intent = getIntent();
        final String news_url = intent.getStringExtra("url");
        final String news_title = intent.getStringExtra("title");
        final String news_date = intent.getStringExtra("date");
        final String news_author = intent.getStringExtra("author");
        final String news_picurl = intent.getStringExtra("pic_url");
        show_news.loadUrl(news_url);

        //返回键功能
        web_tab_arrow_left_second = (Button) findViewById(R.id.web_tab_arrow_left_second);
        web_tab_arrow_left_second.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                finish();
            }
        });

    }

    private void initView() {
        show_news =(WebView) findViewById(R.id.show_news);
    }
}